<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Android," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="Rx指的是ReactiveX，也是Reactive Extensions的缩写，是一个使用可观察数据流进行异步编程的编程接口，结合了观察者模式、迭代器模式和函数式编程的精华，是一种编程思想的突破，它影响了许多其它的程序库和框架以及编程语言 RxJava里最核心的两个东西Observable和Subscriber，Observable指的是被观察者、事件源，Subscriber指的是被观察者、订阅者">
<meta name="keywords" content="Android">
<meta property="og:type" content="article">
<meta property="og:title" content="带大家进入RxJava的世界">
<meta property="og:url" content="http://soaic.github.io/2016/09/23/2016-09-23-rxjava/index.html">
<meta property="og:site_name" content="Soaic&#39;s Blog">
<meta property="og:description" content="Rx指的是ReactiveX，也是Reactive Extensions的缩写，是一个使用可观察数据流进行异步编程的编程接口，结合了观察者模式、迭代器模式和函数式编程的精华，是一种编程思想的突破，它影响了许多其它的程序库和框架以及编程语言 RxJava里最核心的两个东西Observable和Subscriber，Observable指的是被观察者、事件源，Subscriber指的是被观察者、订阅者">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-08-17T17:49:01.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="带大家进入RxJava的世界">
<meta name="twitter:description" content="Rx指的是ReactiveX，也是Reactive Extensions的缩写，是一个使用可观察数据流进行异步编程的编程接口，结合了观察者模式、迭代器模式和函数式编程的精华，是一种编程思想的突破，它影响了许多其它的程序库和框架以及编程语言 RxJava里最核心的两个东西Observable和Subscriber，Observable指的是被观察者、事件源，Subscriber指的是被观察者、订阅者">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://soaic.github.io/2016/09/23/2016-09-23-rxjava/"/>





  <title>带大家进入RxJava的世界 | Soaic's Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Soaic's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">Soaic</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-music">
          <a href="/music/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-music"></i> <br />
            
            音乐
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://soaic.github.io/2016/09/23/2016-09-23-rxjava/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Soaic">
      <meta itemprop="description" content="Welcome!">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Soaic's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">带大家进入RxJava的世界</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-09-23T00:00:00+08:00">
                2016-09-23
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android框架/" itemprop="url" rel="index">
                    <span itemprop="name">Android框架</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2016/09/23/2016-09-23-rxjava/" class="leancloud_visitors" data-flag-title="带大家进入RxJava的世界">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数 </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Rx指的是ReactiveX，也是Reactive Extensions的缩写，是一个使用可观察数据流进行异步编程的编程接口，结合了观察者模式、迭代器模式和函数式编程的精华，是一种编程思想的突破，它影响了许多其它的程序库和框架以及编程语言</p>
<p>RxJava里最核心的两个东西Observable和Subscriber，Observable指的是被观察者、事件源，Subscriber指的是被观察者、订阅者、用户。</p>
<p>举个简单的例子，像微信里面的服务号和公众号，我们关注了某个服务号或公众号，只要发出消息，我们都能同时收到。这里的服务号和公众号就是Observable，Subscriber指的就是我们用户。</p>
<p>看起来RxJava蛮像观察者模式，不过有一点不同Observable如果没有Subscriber不会发出任何事件</p>
<a id="more"></a>
<p>下面就让我们一起进入RxJava的世界吧</p>
<h3 id="参考资料学习网址"><a href="#参考资料学习网址" class="headerlink" title="参考资料学习网址"></a>参考资料学习网址</h3><p>1、<a href="http://blog.danlew.net/2014/09/15/grokking-rxjava-part-1/" target="_blank" rel="external">Grokking RxJava</a><br>2、<a href="https://github.com/ReactiveX/RxJava" target="_blank" rel="external">github地址</a><br>3、<a href="https://mcxiaoke.gitbooks.io/rxdocs/content/" target="_blank" rel="external">ReactiveX/RxJava文档中文版</a><br>4、<a href="http://reactivex.io/" target="_blank" rel="external">ReactiveX官网</a><br>5、<a href="http://gank.io/post/560e15be2dca930e00da1083" target="_blank" rel="external">抛物线博客</a></p>
<h3 id="配置环境"><a href="#配置环境" class="headerlink" title="配置环境"></a>配置环境</h3><p>在builde.gradle里面添加 </p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><div class="line">compile <span class="string">'io.reactivex:rxandroid:1.2.1'</span></div><div class="line">compile <span class="string">'io.reactivex:rxjava:1.1.6'</span></div></pre></td></tr></table></figure>
<h3 id="基本实现"><a href="#基本实现" class="headerlink" title="基本实现"></a>基本实现</h3><p>1、Hello world   </p>
<p>创建Observable对象，通过Observable.create()来创建，sub.onNext()可以发出信息，最后调用sub.onCompleted()来完成，还有一个sub.onError()方法是出现异常提供的一个方法，一旦调用了sub.onError()或者sub.onCompleted()，后面的逻辑代码都不会执行。</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><div class="line">Observable&lt;<span class="built_in">String</span>&gt; myObservable = Observable.create(</div><div class="line">        <span class="keyword">new</span> Observable.OnSubscribe&lt;<span class="built_in">String</span>&gt;() &#123;</div><div class="line">            @Override</div><div class="line">            public <span class="keyword">void</span> call(Subscriber&lt;? <span class="keyword">super</span> <span class="built_in">String</span>&gt; sub) &#123;</div><div class="line">                sub.onNext(<span class="string">"Hello, world1!"</span>);</div><div class="line">                sub.onNext(<span class="string">"Hello, world2!"</span>);</div><div class="line">                sub.onCompleted();</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">);</div></pre></td></tr></table></figure>
<p>创建Subscriber对象，实例化Subscriber抽象类，实现onNext、onCompleted、onError三个方法，依次响应Observable对象里面的sub.onNext()、sub.onCompleted()和sub.onError()方法</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><div class="line">Subscriber&lt;<span class="built_in">String</span>&gt; mySubscriber = <span class="keyword">new</span> Subscriber&lt;<span class="built_in">String</span>&gt;() &#123;</div><div class="line">    @Override</div><div class="line">    public <span class="keyword">void</span> onNext(<span class="built_in">String</span> s) &#123;</div><div class="line">        Log.d(<span class="string">"rxjava"</span>,<span class="string">"onNext="</span>+s); </div><div class="line">    &#125;</div><div class="line">    @Override</div><div class="line">    public <span class="keyword">void</span> onCompleted() &#123; </div><div class="line">        Log.d(<span class="string">"rxjava"</span>,<span class="string">"onCompleted"</span>);</div><div class="line">    &#125;</div><div class="line">    @Override</div><div class="line">    public <span class="keyword">void</span> onError(Throwable e) &#123;</div><div class="line">        Log.d(<span class="string">"rxjava"</span>,<span class="string">"onError="</span>+e);</div><div class="line">    &#125;</div><div class="line">&#125;;</div></pre></td></tr></table></figure>
<p>最后Subscriber订阅Observable，这里Observable可以被多个Subscriber订阅</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><div class="line">myObservable.subscribe(mySubscriber);</div><div class="line">myObservable.subscribe(mySubscriber2);</div><div class="line">myObservable.subscribe(mySubscriber3);</div></pre></td></tr></table></figure>
<p>2、简化代码</p>
<p>上面的代码可以转变为这样：</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><div class="line">Observable&lt;<span class="built_in">String</span>&gt; myObservable = Observable.just(<span class="string">"Hello, world1!"</span>，<span class="string">"Hello, world2!"</span>);</div><div class="line">Action1&lt;<span class="built_in">String</span>&gt; onNextAction = <span class="keyword">new</span> Action1&lt;<span class="built_in">String</span>&gt;() &#123;</div><div class="line">    @Override</div><div class="line">    public <span class="keyword">void</span> call(<span class="built_in">String</span> s) &#123;</div><div class="line">        System.out.println(s);</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line">Action1&lt;Throwable&gt; onErrorAction = <span class="keyword">new</span> Action1&lt;Throwable&gt;() &#123;</div><div class="line">    @Override</div><div class="line">    public <span class="keyword">void</span> call(Throwable e) &#123;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line">Action0 onCompletedAction = <span class="keyword">new</span> Action0() &#123;</div><div class="line">    @Override</div><div class="line">    public <span class="keyword">void</span> call() &#123;</div><div class="line">    &#125;</div><div class="line">&#125;;</div><div class="line"><span class="comment">// 自动创建 Subscriber ，并使用 onNextAction 来定义 onNext()</span></div><div class="line">myObservable.subscribe(onNextAction);</div><div class="line"><span class="comment">// 自动创建 Subscriber ，并使用 onNextAction 和 onErrorAction 来定义 onNext() 和 onError()</span></div><div class="line">myObservable.subscribe(onNextAction,onErrorAction);</div><div class="line"><span class="comment">// 自动创建 Subscriber ，并使用 onNextAction、 onErrorAction 和 onCompletedAction 来定义 onNext()、 onError() 和 onCompleted()</span></div><div class="line">myObservable.subscribe(onNextAction,onErrorAction,onCompletedAction);</div></pre></td></tr></table></figure>
<p><code>Observable.just(&quot;Hello, world1!&quot;，&quot;Hello, world2!&quot;)</code>会直接创建Observable对象，<br>然后依次调用<code>sub.onNext(&quot;Hello, world1!&quot;); sub.onNext(&quot;Hello, world2!&quot;); sub.onCompleted();</code></p>
<p>一般onErrorAction和onCompletedAction我们可以不用管理 最后代码可优化成：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">Observable.just(<span class="string">"Hello, world1!"</span>,<span class="string">"Hello, world2!"</span>)</div><div class="line">            .subscribe(<span class="keyword">new</span> Action1&lt;String&gt;() &#123;</div><div class="line">                <span class="meta">@Override</span></div><div class="line">                <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(String s)</span> </span>&#123;</div><div class="line">                    System.out.println(s);</div><div class="line">                &#125;</div><div class="line">            &#125;);</div></pre></td></tr></table></figure>
<p>java8语法lambda可以写成</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">Observable.just(<span class="string">"Hello, world!"</span>).subscribe(s -&gt; System.out.println(s));</div></pre></td></tr></table></figure>
<p>3、操作符map</p>
<p>简单的一个例子，传递一个字符串，然后获取它的长度，最后打印出来，在RxJava里我们可以这样实现：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">Observable.just(<span class="string">"Hello, world!"</span>)</div><div class="line">        .map(<span class="keyword">new</span> Func1&lt;String,Integer&gt;()&#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> Integer <span class="title">call</span><span class="params">(String s)</span></span>&#123;</div><div class="line">                <span class="keyword">return</span> s.length();</div><div class="line">            &#125;</div><div class="line">        &#125;)</div><div class="line">        .subscribe(<span class="keyword">new</span> Action1&lt;Integer&gt;()&#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(Integer i)</span></span>&#123;</div><div class="line">                System.out.println(Integer.toString(i));</div><div class="line">            &#125;</div><div class="line">        &#125;);</div></pre></td></tr></table></figure>
<p>这里map方法需要结合Func1接口来使用，Func1里第一个String类型是对应接收just类型，第二个Integer类型是返回值，供下一个对象接收</p>
<p>4、操作符from</p>
<p>如果我们要循环遍历一个数组或者集合，在RxJava里面可以使用from操作符来实现</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">String[] items =  &#123; <span class="string">"0"</span>, <span class="string">"1"</span>, <span class="string">"2"</span>, <span class="string">"3"</span>, <span class="string">"4"</span>, <span class="string">"5"</span> &#125; ;</div><div class="line">        Observable.from(items)</div><div class="line">                .subscribe(<span class="keyword">new</span> Action1&lt;String&gt;()&#123;</div><div class="line">                    <span class="meta">@Override</span></div><div class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(String str)</span></span>&#123;</div><div class="line">                        <span class="comment">//依次遍历打印items</span></div><div class="line">                        System.out.println(str);</div><div class="line">                    &#125;</div><div class="line">                &#125;);</div></pre></td></tr></table></figure>
<p>操作符from会循环遍历方法参数里面的数组或者集合，然后依次调用onNext()方法</p>
<p>5、操作符flatMap</p>
<p>如果我们要实现多重循环，可以使用flatMap操作符来实现</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">String[][] itemArray = &#123; &#123;<span class="string">"1"</span>,<span class="string">"2"</span>,<span class="string">"3"</span>&#125;,&#123;<span class="string">"4"</span>,<span class="string">"5"</span>,<span class="string">"6"</span>&#125;,&#123;<span class="string">"7"</span>,<span class="string">"8"</span>,<span class="string">"9"</span>&#125; &#125;;</div><div class="line">Observable.from(itemArray)</div><div class="line">        .flatMap(<span class="keyword">new</span> Func1&lt;String[],Observable&lt;String&gt;&gt;()&#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> Observable&lt;String&gt; <span class="title">call</span><span class="params">(String[] s)</span></span>&#123;</div><div class="line">                <span class="keyword">return</span> Observable.from(s);</div><div class="line">            &#125;</div><div class="line">        &#125;)</div><div class="line">        .subscribe(<span class="keyword">new</span> Subscriber&lt;String&gt;()&#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span></span>&#123;</div><div class="line">            &#125;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span></span>&#123;</div><div class="line">            &#125;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(String str)</span></span>&#123;</div><div class="line">                Log.d(<span class="string">"rxJava"</span>,<span class="string">"str="</span>+str);</div><div class="line">            &#125;</div><div class="line">        &#125;);</div></pre></td></tr></table></figure>
<p>上面代码执行后，依次打印str=1到9，这里要注意的是Func1第二个参数类型是Observable&lt;?&gt;</p>
<p>6、操作符filter</p>
<p>字面理解应该就知道它的作用了，过滤不需要的数据</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">Observable.just(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>)</div><div class="line">    .filter(<span class="keyword">new</span> Func1&lt;Integer, Boolean&gt;() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> Boolean <span class="title">call</span><span class="params">(Integer item)</span> </span>&#123;</div><div class="line">            <span class="keyword">return</span>( item &lt; <span class="number">4</span> );</div><div class="line">        &#125;</div><div class="line">    &#125;)</div><div class="line">    .subscribe(<span class="keyword">new</span> Subscriber&lt;Integer&gt;() &#123;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(Integer item)</span> </span>&#123;</div><div class="line">            System.out.println(<span class="string">"Next: "</span> + item);</div><div class="line">        &#125;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable error)</span> </span>&#123;</div><div class="line">            System.err.println(<span class="string">"Error: "</span> + error.getMessage());</div><div class="line">        &#125;</div><div class="line">        <span class="meta">@Override</span></div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</div><div class="line">            System.out.println(<span class="string">"Sequence complete."</span>);</div><div class="line">        &#125;</div><div class="line">    &#125;);</div></pre></td></tr></table></figure>
<p>上面代码执行结果，只会打印1到3，后面4和5不符合，则过滤</p>
<p>7、操作符merge</p>
<p>合并多个Observables</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">Observable&lt;Integer&gt; odds = Observable.just(<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>);</div><div class="line">Observable&lt;Integer&gt; evens = Observable.just(<span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>);</div><div class="line">Observable.merge(odds, evens)</div><div class="line">        .subscribe(<span class="keyword">new</span> Subscriber&lt;Integer&gt;() &#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(Integer item)</span> </span>&#123;</div><div class="line">                System.out.println(<span class="string">"Next: "</span> + item);</div><div class="line">            &#125;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable error)</span> </span>&#123;</div><div class="line">                System.err.println(<span class="string">"Error: "</span> + error.getMessage());</div><div class="line">            &#125;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</div><div class="line">                System.out.println(<span class="string">"Sequence complete."</span>);</div><div class="line">            &#125;</div><div class="line">        &#125;);</div></pre></td></tr></table></figure>
<p>上面代码执行后结果：依次打印的是1、3、5、2、4、6</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">*  Javadoc: merge(Iterable)</div><div class="line">*  Javadoc: merge(Iterable,<span class="keyword">int</span>)</div><div class="line">*  Javadoc: merge(Observable[])</div><div class="line">*  Javadoc: merge(Observable,Observable) (接受二到九个Observable)</div></pre></td></tr></table></figure>
<p>除了传递多个Observable给merge，你还可以传递一个Observable列表List，数组，甚至是一个发射Observable序列的Observable，merge将合并它们的输出作为单个Observable的输出</p>
<p>8、操作符timer和interval</p>
<p>5秒后打印数据，第一个参数是多少时间执行，第二个参数是时间单位</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">Observable.timer(<span class="number">5</span>,TimeUnit.SECONDS)</div><div class="line">        .subscribe(<span class="keyword">new</span> Action1&lt;Long&gt;()&#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(Long aLong)</span></span>&#123;</div><div class="line">                Log.d(<span class="string">"rxJava"</span>,<span class="string">"5秒后执行"</span>+aLong);</div><div class="line">            &#125;</div><div class="line">        &#125;);</div></pre></td></tr></table></figure>
<p>1秒后每5秒循环打印，第一个参数为多少时间后执行，第二个为没多少时间执行，第三个是时间单位</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">Observable.interval(<span class="number">1</span>,<span class="number">5</span>,TimeUnit.SECONDS)</div><div class="line">        .subscribe(<span class="keyword">new</span> Action1&lt;Long&gt;()&#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(Long aLong)</span></span>&#123;</div><div class="line">                Log.d(<span class="string">"rxJava"</span>,<span class="string">"1秒后每5秒循环执行"</span>+aLong);</div><div class="line">            &#125;</div><div class="line">        &#125;);</div></pre></td></tr></table></figure>
<p>这里aLong返回的值应该是打印的次数</p>
<p>9、线程切换</p>
<p>主要通过subscribeOn()和 observeOn()来实现<br><code>subscribeOn()</code>是控制Observable.OnSubscribe的线程切换， subscribeOn()的线程切换发生在 OnSubscribe中，即在它通知上一级 OnSubscribe时，这时事件还没有开始发送，因此控制可以从事件发出的开端就造成影响<br><code>observeOn()</code>则是控制Subscriber的线程切换，observeOn() 的线程切换则发生在它内建的 Subscriber中，即发生在它即将给下一级 Subscriber发送事件时，因此控制的是它后面的线程<br><code>Schedulers.io()</code>：IO线程<br><code>AndroidSchedulers.mainThread()</code>：主线程<br><code>Schedulers.newThread()</code>：新开线程</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">Observable.just(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>) <span class="comment">// IO 线程，由 subscribeOn() 指定</span></div><div class="line">    .subscribeOn(Schedulers.io())</div><div class="line">    .observeOn(Schedulers.newThread())</div><div class="line">    .map(mapOperator) <span class="comment">// 新线程，由 observeOn() 指定</span></div><div class="line">    .observeOn(Schedulers.io())</div><div class="line">    .map(mapOperator2) <span class="comment">// IO 线程，由 observeOn() 指定</span></div><div class="line">    .observeOn(AndroidSchedulers.mainThread) </div><div class="line">    .subscribe(subscriber);  <span class="comment">// Android 主线程，由 observeOn() 指定</span></div></pre></td></tr></table></figure>
<p>10、防止按钮重复点击</p>
<p>1秒钟内只会执行第一次点击</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">RxView.clicks(view)</div><div class="line">        .throttleFirst(<span class="number">1</span>,TimeUnit.SECONDS)</div><div class="line">        .subscribe(<span class="keyword">new</span> Observer&lt;Object&gt;()&#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span></span>&#123;</div><div class="line">            &#125;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span></span>&#123;</div><div class="line">            &#125;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(Object o)</span></span>&#123;</div><div class="line">            &#125;</div><div class="line">        &#125;);</div></pre></td></tr></table></figure>
<p>RxView是RxBinding库里的对象，需要添加环境</p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><div class="line">compile <span class="string">'com.jakewharton.rxbinding:rxbinding:0.4.0'</span></div></pre></td></tr></table></figure>
<p>throttleFirst():在每次事件触发后的一定时间间隔内丢弃新的事件</p>
<p>11、RxBus代替EventBus</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RxBus</span> </span>&#123;</div><div class="line">    <span class="comment">// 主题</span></div><div class="line">    <span class="keyword">private</span> <span class="keyword">final</span> Subject&lt;Object, Object&gt; bus;</div><div class="line">    <span class="comment">// PublishSubject只会把在订阅发生的时间点之后来自原始Observable的数据发射给观察者</span></div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="title">RxBus</span><span class="params">()</span> </span>&#123;</div><div class="line">        bus = <span class="keyword">new</span> SerializedSubject&lt;&gt;(PublishSubject.create());</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> RxBus <span class="title">get</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> RxBusHolder.sInstance;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">unSubscribe</span><span class="params">(CompositeSubscription compositeSubscription)</span></span>&#123;</div><div class="line">        <span class="keyword">if</span> (compositeSubscription != <span class="keyword">null</span> &amp;&amp; !compositeSubscription.isUnsubscribed())</div><div class="line">            compositeSubscription.unsubscribe();</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">RxBusHolder</span> </span>&#123;</div><div class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> RxBus sInstance = <span class="keyword">new</span> RxBus();</div><div class="line">    &#125;</div><div class="line">    <span class="comment">// 提供了一个新的事件</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">post</span><span class="params">(Object o)</span> </span>&#123;</div><div class="line">        bus.onNext(o);</div><div class="line">    &#125;</div><div class="line">    <span class="comment">// 根据传递的 eventType 类型返回特定类型(eventType)的 被观察者</span></div><div class="line">    <span class="keyword">public</span> &lt;T&gt; <span class="function">Observable&lt;T&gt; <span class="title">toObserverable</span><span class="params">(Class&lt;T&gt; eventType)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> bus.ofType(eventType);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>RxBus提供了三个方法toObserverable()获取Observable对象、unSubscribe()解除订阅、post()发送数据。</p>
<p>在MainActivity中实现</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> CompositeSubscription allSubscription = <span class="keyword">new</span> CompositeSubscription();</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span></span>&#123;</div><div class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</div><div class="line">        setContentView(R.layout.activity_main);</div><div class="line">        ...</div><div class="line">        initRxBus();</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initRxBus</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="comment">//注册订阅RxBus并添加到CompositeSubscription,在onDestroy()里可以统一解除订阅</span></div><div class="line">        allSubscription.add(RxBus.get().toObserverable(OneEvent.class).subscribe(<span class="keyword">this</span>::response));</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onDestroy</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">super</span>.onDestroy();</div><div class="line">        <span class="comment">//解除订阅</span></div><div class="line">        RxBus.get().unSubscribe(allSubscription);</div><div class="line">    &#125;</div><div class="line">    <span class="comment">/**</span></div><div class="line"><span class="comment">     * RxBus响应接收数据方法</span></div><div class="line"><span class="comment">     * <span class="doctag">@param</span> event</span></div><div class="line"><span class="comment">     */</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">response</span><span class="params">(OneEvent event)</span> </span>&#123;</div><div class="line">        Toast.makeText(getApplicationContext(),event.msg,Toast.LENGTH_LONG).show();</div><div class="line">    &#125;</div><div class="line">    <span class="class"><span class="keyword">class</span> <span class="title">OneEvent</span> </span>&#123;</div><div class="line">        String msg;</div><div class="line">        <span class="function"><span class="keyword">public</span> <span class="title">OneEvent</span><span class="params">(String msg)</span> </span>&#123;</div><div class="line">            <span class="keyword">this</span>.msg = msg;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>最后在需要发送数据的地方调用，跟EventBus还是满相似的</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">RxBus.get().post(<span class="keyword">new</span> OneEvent(<span class="string">"hello bus"</span>));</div></pre></td></tr></table></figure>
<p>12、结合Retrofit使用<br>这里我就直接拿介绍Retrofit那篇文章的例子在这里说明下</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="meta">@POST</span>(<span class="string">"query"</span>)</div><div class="line"><span class="function">Observable&lt;PostQueryInfo&gt; <span class="title">searchRx</span><span class="params">(@Query(<span class="string">"type"</span>)</span> String type, @<span class="title">Query</span><span class="params">(<span class="string">"postid"</span>)</span> String postid)</span>;</div></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">Retrofit retrofit = <span class="keyword">new</span> Retrofit.Builder()</div><div class="line">        .baseUrl(<span class="string">"http://www.kuaidi100.com/"</span>)</div><div class="line">         <span class="comment">//添加数据解析ConverterFactory</span></div><div class="line">        .addConverterFactory(GsonConverterFactory.create()) </div><div class="line">         <span class="comment">//添加RxJava</span></div><div class="line">        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())   </div><div class="line">        .build();</div><div class="line">GitHubService apiService = retrofit.create(GitHubService.class);</div><div class="line">apiService.searchRx(<span class="string">"yuantong"</span>,<span class="string">"500379523313"</span>)</div><div class="line">		<span class="comment">//访问网络切换异步线程</span></div><div class="line">        .subscribeOn(Schedulers.io())</div><div class="line">        <span class="comment">//响应结果处理切换成主线程</span></div><div class="line">        .observeOn(AndroidSchedulers.mainThread())</div><div class="line">        .subscribe(<span class="keyword">new</span> Subscriber&lt;PostQueryInfo&gt;() &#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</div><div class="line">            	 <span class="comment">//请求结束回调</span></div><div class="line">            &#125;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span> </span>&#123;</div><div class="line">            	 <span class="comment">//错误回调</span></div><div class="line">                e.printStackTrace();</div><div class="line">            &#125;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(PostQueryInfo postQueryInfo)</span> </span>&#123;</div><div class="line">            	 <span class="comment">//成功结果返回</span></div><div class="line">                Log.e(<span class="string">"APP"</span>,postQueryInfo.getNu());</div><div class="line">            &#125;</div><div class="line">        &#125;);</div></pre></td></tr></table></figure>
<p>Retrofit支持RxJava可以返回Observable对象，所以我们就可以直接拿来用，很方便吧<br><code>.subscribeOn(Schedulers.io()</code>请求网络切换成IO线程<br><code>.observeOn(AndroidSchedulers.mainThread())</code>切换成主线程更新UI<br><code>onNext(PostQueryInfo postQueryInfo)</code>成功回调解析数据<br><code>public void onError(Throwable e)</code>异常回调抛出异常</p>
<h3 id="Tips"><a href="#Tips" class="headerlink" title="Tips"></a>Tips</h3><p>配置java8 lambda环境  </p>
<p>1、在Module中的build.gradle中添加，然后Make Module ‘xxx’   </p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><div class="line">apply plugin: <span class="string">'me.tatarka.retrolambda'</span></div><div class="line">buildscript &#123;</div><div class="line">    repositories &#123;</div><div class="line">        mavenCentral()</div><div class="line">    &#125;</div><div class="line">    dependencies &#123;</div><div class="line">        classpath <span class="string">'me.tatarka:gradle-retrolambda:3.2.5'</span></div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">repositories &#123;</div><div class="line">    mavenCentral()</div><div class="line">&#125;</div><div class="line">android &#123;</div><div class="line">	compileOptions &#123;</div><div class="line">        sourceCompatibility JavaVersion.VERSION_1_8</div><div class="line">        targetCompatibility JavaVersion.VERSION_1_8</div><div class="line">    &#125;</div><div class="line">	...</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>2、测试 下面代码不报红，配置环境OK</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line">textView.setOnClickListener(v -&gt; </div><div class="line">	Toast.makeText(getApplicationContext(),<span class="string">"lambda"</span>,Toast.LENGTH_LONG).show()</div><div class="line">);</div></pre></td></tr></table></figure>
<p>学到最后其实RxJava只是刚入了个门，还有很多很多需要我们去学习的，这里只是跟大家一起看了看RxJava的世界是什么样子的，如果要学好，还是要多去看看api文档，多练习才能真正的熟练使用它。</p>
<p><strong>革命尚未成功，我们仍需努力</strong></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Android/" rel="tag"># Android</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/09/05/2016-09-06-mvp/" rel="next" title="你应该学会的设计模式MVP">
                <i class="fa fa-chevron-left"></i> 你应该学会的设计模式MVP
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/12/13/2016-12-13-greendao/" rel="prev" title="你必须了解的ORM框架GreenDao">
                你必须了解的ORM框架GreenDao <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="Soaic" />
          <p class="site-author-name" itemprop="name">Soaic</p>
           
              <p class="site-description motion-element" itemprop="description">Welcome!</p>
           
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">10</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">1</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#参考资料学习网址"><span class="nav-number">1.</span> <span class="nav-text">参考资料学习网址</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置环境"><span class="nav-number">2.</span> <span class="nav-text">配置环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#基本实现"><span class="nav-number">3.</span> <span class="nav-text">基本实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Tips"><span class="nav-number">4.</span> <span class="nav-text">Tips</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2016 - 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Soaic</span>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  






  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("nSNhFcJCeysT9vRXgfNvtbQF-gzGzoHsz", "aegQNi8pntiOE4R2z0e5B84D");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  

  

</body>
</html>
